package com.ruiben.mall.controller;


import com.ruiben.mall.impl.BannerServiceImpl;
import com.ruiben.mall.impl.ProductServiceImpl;
import com.ruiben.mall.pojo.Banner;
import com.ruiben.mall.pojo.ProductCategory;
import com.ruiben.mall.pojo.ShopProduct;
import com.ruiben.mall.utils.CommonJSONResult;
import com.ruiben.mall.utils.PagedResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.ruiben.mall.config.Config.BASE_URL;
import static com.ruiben.mall.controller.BaseController.PAGE_SIZE;

@RequestMapping("index")
@RestController
public class IndexController extends BaseController{


    @Autowired
    public BannerServiceImpl bannerService;

    @Autowired
    public ProductServiceImpl productService;

    @PostMapping("index")
    public CommonJSONResult index(){
        Map<String,Object> indexMap = new HashMap<>();

        List<Banner> bannerList = bannerService.getBannerList();
        for (Banner banner:bannerList){
            banner.setImagePath(BASE_URL + banner.getImagePath());
        }
        indexMap.put("banner",bannerList);

        //获取推荐商品
        List<ShopProduct> recommendList = productService.getRecommendList();
        for (ShopProduct shopProduct:recommendList){
            shopProduct.setImagePath(BASE_URL + shopProduct.getImagePath());
        }
        indexMap.put("recommendList",recommendList);


        //获取推荐商品
        List<ShopProduct> hotList = productService.getHotList();
        for (ShopProduct shopProduct:hotList){
            shopProduct.setImagePath(BASE_URL + shopProduct.getImagePath());
        }
        indexMap.put("hotList",hotList);


        //获取一级分类
        List productList = new ArrayList();
        List<ProductCategory> categoryList = productService.getParentProductCategory();
        if (categoryList != null && categoryList.size() > 0) {
            for (ProductCategory category : categoryList) {
                Map<String, Object> productMap = new HashMap<>();
                productMap.put("categoryName", category.getTypename());
                String id = category.getId();
                List<ProductCategory> childProductCategory = productService.getChildProductCategory(id);
                List<String> cateIds = new ArrayList<>();
                for (ProductCategory child : childProductCategory) {
                    cateIds.add(child.getId());
                }
                if (cateIds.size() > 0) {

                    List<ShopProduct> productListByAllCateId = productService.getProductListByAllCateId(cateIds);
                    productMap.put("productChildList",productListByAllCateId);
                    productList.add(productMap);
                }
            }

            indexMap.put("productList",productList);

        }


        return CommonJSONResult.ok(indexMap);
    }

    @PostMapping("phone/getIndex")
    public CommonJSONResult getIndex(){

        Map<String,Object> indexMap = new HashMap<>();

        List<Banner> bannerList = bannerService.getBannerList();
        for (Banner banner:bannerList){
            banner.setImagePath(BASE_URL + banner.getImagePath());
        }
        indexMap.put("banner",bannerList);

        //获取最新商品
        List<ShopProduct> recommendList = productService.getNewsGoods();
        for (ShopProduct shopProduct:recommendList){
            shopProduct.setImagePath(BASE_URL + shopProduct.getImagePath());
        }
        indexMap.put("newList",recommendList);


        //获取热卖商品
        List<ShopProduct> hotList = productService.getHotList();
        for (ShopProduct shopProduct:hotList){
            shopProduct.setImagePath(BASE_URL + shopProduct.getImagePath());
        }
        indexMap.put("hotList",hotList);


        //获取分类
        List<ProductCategory> parentProductCategory = productService.getParentProductCategory();
        indexMap.put("category", parentProductCategory);

        //获取所有 商品
        PagedResult productList = productService.getProductList(1, PAGE_SIZE);

        indexMap.put("goodsList",productList);

        return CommonJSONResult.ok(indexMap);
    }


    @PostMapping("getMoreProductList")
    public CommonJSONResult getMoreGoods(Integer page){

        if (page == null) {
            page = 2;
        }
        //获取分类
        PagedResult productList =  productService.getProductList(page,PAGE_SIZE);
        return CommonJSONResult.ok(productList);
    }

}
